const fs = require('fs');
const path = require('path');
const readline = require('node:readline');
let notFound = []

const permissionsList = [
  'mission_tasksOfTheDay',
  'mission_taskList_edit',
  'mission_taskList_assign',
  'mission_taskList_delete',
  'mission_taskList_add'
]
function organizeContent (content) {
  content = content.replace(/INSERT INTO `permission` VALUES \(([\s\S]*?)\);/g, (_,permissionContent = '') => {
    const dataList = permissionContent.split(',')
    dataList[2] = 0
    let  permissions = dataList[6].replace(/\s|'/g, '')
    const permissionsIndex = permissionsList.findIndex(v =>  v === permissions)
    if (permissionsIndex !== -1) {
      return _.replace(permissionContent, dataList.toString())
    } else {
      return ''
    }
  })
  return content
}

async function setVueFile (path) {
  fs.readFile(path, 'utf8', async (err, data) => {
    if (err) {
      return;
    }
    exportContent(organizeContent(data))
  });
}

function exportContent (content) {
  fs.writeFile('./test.sql',content, 'utf8', (err) => {
    if (err) {
      console.error(err);
    }
  });
}

setVueFile('./permission.sql')
